import { AccountResponse } from "./model/model-account";

const TOKEN = 'gc-quiz.account.token';

export default class GCGlobal {

    private static _token: string;

    static get token() {
        return this._token;
    }

    static set token(val: string) {
        this._token = val;
        localStorage.setItem(TOKEN, this._token);
    }

    public static Account: AccountResponse = null;

    static initGlobal() {
        this._token = localStorage.getItem(TOKEN) || '';
    }

    static isSignedIn() {
        return !!this._token;
    }

    static getParamsObj() {
        const search = window.location.search;
        const idx = search.indexOf('?');

        const params = search.slice(idx + 1);
        const pairs = params.split('&');

        const obj = {};

        for (let pair of pairs) {
            const [key, value] = pair.split('=');
            obj[key] = value;
        }

        return obj; 
    }
}

if (CC_DEBUG) {
    (window as any).GCGlobal = GCGlobal;
}
